package lrh.hubu.firstproject;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;

public class talkFragment extends Fragment {

    private View view;
    private ArrayList<String> mylist = new ArrayList<>();
    private ArrayList<mydata> myDataList = new ArrayList<mydata>();

    public talkFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.t3, container, false);
        // Inflate the layout for this fragment
        initdata();

        initData();

        initView();

        return view;
    }

    private void initdata(){
        mylist.add("艾欧尼亚,进击的小学生");
        mylist.add("艾欧尼亚,进击的中学生");
        mylist.add("艾欧尼亚,进击的高中生");
        mylist.add("艾欧尼亚,进击的大学生");
        mylist.add("艾欧尼亚,进击的毕业生");
        mylist.add("黑色玫瑰,我们是黑店");
        mylist.add("黑色玫瑰,我们真是黑店");
        mylist.add("黑色玫瑰,别不信我们是黑店");
        mylist.add("黑色玫瑰,相信我们是黑店");
        mylist.add("黑色玫瑰,还不信我们是黑店");
        mylist.add("班德尔城,打野没意识");
        mylist.add("班德尔城,中单不游走");
        mylist.add("班德尔城,上单送人头");
        mylist.add("班德尔城,辅助卖队友");
        mylist.add("班德尔城,adc打钱慢");
    }

    private void initData() {
        for (int i = 0; i < mylist.size(); i++) {
            mydata bean = new mydata();

            String s = mylist.get(i);
            // group
            String group = s.substring(0, s.indexOf(","));
            // name
            String team = s.substring(s.indexOf(",") + 1, s.length());

            bean.setGroup(group);
            bean.setName(team);

            myDataList.add(bean);
        }
    }

    private void initView() {
        talkAdapter adapter = new talkAdapter(getActivity());

        final TextView tvGroup = view.findViewById(R.id.t3_top);
        RecyclerView RecyclerView = view.findViewById(R.id.recyclerview3);

        ItemTouchHelper.Callback callback = new talkItemTouchHelper(adapter);
        ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
        touchHelper.attachToRecyclerView(RecyclerView);

        RecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        RecyclerView.setHasFixedSize(true);
        RecyclerView.setAdapter(adapter);

        adapter.setDragDataList(myDataList);
        RecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                View stickyInfoView = recyclerView.findChildViewUnder(tvGroup.getMeasuredWidth() / 2, 5);

                if (stickyInfoView != null && stickyInfoView.getContentDescription() != null) {
                    tvGroup.setText(String.valueOf(stickyInfoView.getContentDescription()));
                }

                View transInfoView = recyclerView.findChildViewUnder(
                        tvGroup.getMeasuredWidth() / 2, tvGroup.getMeasuredHeight() + 1);

                if (transInfoView != null && transInfoView.getTag() != null) {

                    int transViewStatus = (int) transInfoView.getTag();
                    int dealtY = transInfoView.getTop() - tvGroup.getMeasuredHeight();

                    if (transViewStatus == GridAdapter.HAS_STICKY_VIEW) {
                        if (transInfoView.getTop() > 0) {
                            tvGroup.setTranslationY(dealtY);
                        } else {
                            tvGroup.setTranslationY(0);
                        }
                    } else if (transViewStatus == GridAdapter.NONE_STICKY_VIEW) {
                        tvGroup.setTranslationY(0);
                    }
                }
            }
        });
    }

}